package com.Main.faislamovers;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.MarshalBase64;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.Classes.Order;
import com.Util.OrdersAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class ViewBusesSchedule extends Activity {
	
	
	ListView listview;
	OrdersAdapter adapter;

	String NAMESPACE = "http://tempuri.org/";
	String METHODNAME = "FindBuses";
	String url = "http://10.0.2.2:53512/Service1.asmx";
	String SopaAction = "http://tempuri.org/FindBuses";
	
	

	static String result22;
	static String source_new, destination_new;

	ProgressDialog dialog;
	ProgressBar progressBar;

	String source, destination, date;

	public ArrayList<Order> myOrderLIst = new ArrayList<Order>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.find_busses);

		result22 = com.Main.faislamovers.Login.result;
		source_new = com.Main.faislamovers.Login.txt_edt_source;
		destination_new = com.Main.faislamovers.Login.txt_edt_dertination;

		// Get Data from Intents
		source = getIntent().getStringExtra(AppConstants.SOURCE);
		destination = getIntent().getStringExtra(AppConstants.DESTINATION);
		date = getIntent().getStringExtra(AppConstants.DATE);

		listview = (ListView) findViewById(R.id.mainListView);

		adapter = new OrdersAdapter(this, R.layout.listview_item_row,
				myOrderLIst);

		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String trvl_id;
				// int id_trvel;
				SharedPreferences prefs = getSharedPreferences(
						AppConstants.PREFERENCES_NAME, 0);
				trvl_id = myOrderLIst.get(position).travel_id;

				Editor editior = prefs.edit();
				editior.putString(AppConstants.TRAVEL_ID, trvl_id);
				editior.commit();

/*				int duration = Toast.LENGTH_SHORT;
				Toast.makeText(getBaseContext(),
						myOrderLIst.get(position).route_to, duration).show();
				Intent intent = new Intent(ViewBusesSchedule.this, FromTo.class);
				intent.putExtra(AppConstants.SEATS_LIST,
						myOrderLIst.get(position).seatsList);
				startActivity(intent);*/
			}
		});
		dialog = new ProgressDialog(ViewBusesSchedule.this);
		dialog.setMessage("Loading...");
		dialog.show();
		new Thread(new Runnable() {
			public void run() {

				SoapObject request = new SoapObject(NAMESPACE, METHODNAME);
				request.addProperty(AppConstants.SOURCE, source);
				request.addProperty(AppConstants.DESTINATION, destination);
				request.addProperty(AppConstants.DATE, date);

				SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
						SoapEnvelope.VER11);
				envelope.dotNet = true;
				MarshalBase64 m = new MarshalBase64();
				m.register(envelope);
				envelope.encodingStyle = SoapEnvelope.ENC;
				// send request
				envelope.setOutputSoapObject(request);
				HttpTransportSE androidHttpTransport = new HttpTransportSE(url);
				try {
					androidHttpTransport.call(SopaAction, envelope);
				} catch (IOException e) {

					e.printStackTrace();
				} catch (XmlPullParserException e) {

					e.printStackTrace();
				}

				String rsult = null;
				try {
					rsult = envelope.getResponse().toString();
				} catch (SoapFault e) {

					e.printStackTrace();
				}
				parseJSON(rsult);

				runOnUiThread(new Runnable() {

					@Override
					public void run() {

						listview.setAdapter(adapter);
						if (dialog.isShowing())
							dialog.hide();
					}
				});
			}
		}).start();
	}

	private void addProgressBar() {
		progressBar = new ProgressBar(this, null,
				android.R.attr.progressBarStyleSmall);
		this.addContentView(progressBar, new LayoutParams(50, 50));
		// progressBar.startAnimation(null);
	}

	public void parseJSON(String json) {

		Type colcitonType = new TypeToken<ArrayList<Order>>() {
		}.getType();

		List<Order> list = new Gson().fromJson(json, colcitonType);

		for (Order o : list) {
			myOrderLIst.add(o);
		}
	}

	private class FetchOrdersAsyncTask extends AsyncTask<Void, Void, Void> {
		ProgressDialog dialog;
		Context ctx;

		public FetchOrdersAsyncTask(Context contxt) {
			this.ctx = contxt;
		}

		@Override
		protected void onPreExecute() {

			dialog = new ProgressDialog(this.ctx);
			dialog.setMessage("Loading...");
			if (!dialog.isShowing())
				dialog.show();

			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Void... params) {

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {

			super.onPostExecute(result);
		}

	}
	

}
